var BACKGROUND_MUSIC = 'BACKGROUND_MUSIC';
var TICK_SOUND = 'TICK_SOUND';
var TRUE_STRING  = 'TRUE';
var FALSE_STRING = 'FALSE';

exports.GAAudioManager = Core.Class.singleton({
    classname: 'GAAudioManager',
    initialize: function() {
      //  this._click = new Audio.ActiveEffect(new Audio.Effect('./Content/audio/trampoline.caf'));
        this._tickEffect = null;       
        this._storage = new Storage.KeyValue();
 		this._bgPath = null;
		this._tickPath = null;
   },
	playBgMusic: function() {
    	if(this._bgPath != null){
			var myself = this;
			this._storage.getItem(BACKGROUND_MUSIC,this._callBack.bind(this));
    	}
    	else{
    	}
    },
    setBGMusicPath : function(path){
    	if(path != null && path.length != 0){
    		this._bgPath = path;
    		 Audio.Music.setPath(path);
    	}else{
    	}
    }, 
    setTickMusicPath : function(path){
    	if(path != null && path.length != 0){
    		this._tickPath = path;
    		if(this._tickEffect != null ){
    			delete this._tickEffect;
    			this._tickEffect = null;
    		}
    		this._tickEffect = new Audio.ActiveEffect(new Audio.Effect(path));
    	}else{
    	}
    },
    pauseBgMusic: function() {
    	Audio.Music.pause();
    },
    stopBgMusic: function() {
		GAAudioManager._storage.setItem(BACKGROUND_MUSIC,FALSE_STRING);
    	Audio.Music.stop();
    },
    _callBack : function(error, value, key) {
    	GAAudioManager._music = false;
    	if(value == null) {
    		Audio.Music.play();
    		GAAudioManager._music = true;
    	}
    	else if(value == TRUE_STRING ) {
    		Audio.Music.play();
    		GAAudioManager._music = true;
    	}
		//GAAudioManager._storage.setItem(BACKGROUND_MUSIC,TRUE_STRING);
    },
    toggleBackgroundMusic :function() {
    	var toggleValue = !(GAAudioManager._music);
		var str = FALSE_STRING;
    	if(toggleValue) {
    		str = TRUE_STRING;
    		Audio.Music.play();
    	}
    	else {
    		Audio.Music.stop();
    	}
    	GAAudioManager._music= toggleValue;
    	this._storage.setItem(BACKGROUND_MUSIC,str);
    },
    /* added by Kashmala Zafar */
    initializeSoundEffect :function() {
    	this._storage.getItem(TICK_SOUND,this._callBackInitializeSound.bind(this));
    },
    _callBackInitializeSound :function(error, value, key) {
    	this._playTickSound = true;
    	if(value == FALSE_STRING) {
    		this._playTickSound = false;
    	}
    },
    /* end added by Kashmala Zafar */
    toggleSoundEffect :function() {
    	this._storage.getItem(TICK_SOUND,this._callBackToggleSound.bind(this));
    },
	_callBackToggleSound : function(error, value, key){
    	this._playTickSound = !(this._playTickSound);//kz
		var str = FALSE_STRING;
		if(value == null) {
			str = TRUE_STRING;
			this._playTickSound = true;//kz
    	}
    	else if(value == FALSE_STRING) {
    		str = TRUE_STRING;
    	}
    	this._storage.setItem(TICK_SOUND,str);   	
    },
    playTickSound: function() {    	
    	if(this._tickPath !=null){
    		this._storage.getItem(TICK_SOUND,this._callBackPlayTickSound.bind(this));
    	}else{
    	}
    },
    _callBackPlayTickSound : function(error, value, key){
    	if(value == TRUE_STRING){
			if(this._tickEffect) {
				this._tickEffect.play();
			}else{	
			}
    	}
    },
    mute : function(){
    	this.pauseBgMusic();
//    	this._tickEffect = null;
    },
    unmute : function() {
//    	this.setTickMusicPath(this._tickPath);
    	this._storage.getItem(BACKGROUND_MUSIC,this._callBackToggle.bind(this));
    },
    _callBackToggle : function(error, value, key){
    	GAAudioManager._music = false;
    	if(value == null) {
    		Audio.Music.play();
    		GAAudioManager._music = true;
    	}
    	else if(value == TRUE_STRING ) {
    		Audio.Music.play();
    		GAAudioManager._music = true;
    	}
    },
    destroy : function() {
    	delete this._tickEffect;
    	this._tickEffect = null
    	this._tick.destroy();
    	this._tick = null;
    	this._storage = null;
    }
});
